package com.fh.controller.information.usercompany;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.service.system.company.CompanyService;
import com.fh.service.system.company.DepartService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.GetPinyin;
import com.fh.util.PageData;

/**
 * 
 * @包名 :com.fh.controller.system.company
 * @文件名 :CompanyController.java
 *      TODO 类作用：公司
 * @Author: 孙超文 386480250@qq.com
 * @Date: 2015-7-2 下午5:37:33
 */
@Controller
@RequestMapping(value = "/usercompany")
public class UserCompanyController extends BaseController {
	@Resource(name = "CompanyService")
	private CompanyService companyService;
	@Resource(name = "DepartService")
	private DepartService departService;

	/**
	 * 
	 * TODO 方法作用：进入公司列表
	 * 
	 * @param session
	 * @param page
	 * @return
	 * @throws Exception
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:42:11
	 */
	@RequestMapping(value = "/list")
	public ModelAndView listUsers(HttpSession session, Page page) throws Exception {
		logBefore(logger, "公司列表");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();

			// 检索条件================================
			String companycode = pd.getString("companycode");
			String companyname = pd.getString("companyname");
			String companypiny = pd.getString("companypiny");
			if ((null != companycode) && !"".equals(companycode)) {
				companycode = companycode.trim();
				pd.put("companycode", companycode);
			}
			if ((null != companyname) && !"".equals(companyname)) {
				companyname = companyname.trim();
				pd.put("companyname", companyname);
			}
			if ((null != companypiny) && !"".equals(companypiny)) {
				companyname = companypiny.trim();
				pd.put("companypiny", companypiny);
			}
			// 检索条件================================
			page.setPd(pd);
			List<PageData> varList = companyService.list(page);

			mv.setViewName("information/company/company_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至详细页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goFind")
	public ModelAndView goFind() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd.put("id", getSessionCompanyid());
			pd = companyService.findById(pd);
			mv.setViewName("information/company/company_from");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至新增页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:42:55
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("information/company/company_edit");
			mv.addObject("msg", "save");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	@RequestMapping(value = "/save")
	public ModelAndView save(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		// 公司简拼为null，系统设置首字母
		if (StringUtils.isEmpty(pd.getString("companypiny"))) {
			pd.put("companypiny", GetPinyin.getPinYinHeadChar(pd.getString("companyname")));
		}
		String companyid = get32UUID();
		pd.put("id", companyid);
		// 省
		pd.put("addresssheng", pd.getString("province"));
		// 市
		pd.put("addressshi", pd.getString("city"));
		// 区
		pd.put("addressqu", pd.getString("county"));
		pd.put("creator", getSessionUserid()); // 新增人
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("createdate", DateUtil.getTime()); // 新增时间
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		companyService.save(pd);
		// 创建网管部门
		pd.put("id", get32UUID());
		pd.put("departcode", "BM_001");
		pd.put("departname", "网管部");
		pd.put("departpiny", "wgb");
		pd.put("companyid", companyid);
		pd.put("bz", "系统生成");
		departService.save(pd);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至修改页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd.put("id", getSessionCompanyid());
			pd = companyService.findById(pd);
			mv.setViewName("information/company/company_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：修改公司信息
	 * 
	 * @param out
	 * @return
	 * @throws Exception
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:46:33
	 */
	@RequestMapping(value = "/edit")
	public ModelAndView edit(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		// 公司简拼为null，系统设置首字母
		if (StringUtils.isEmpty(pd.getString("companypiny"))) {
			pd.put("companypiny", GetPinyin.getPinYinHeadChar(pd.getString("companyname")));
		}
		// 省
		pd.put("addresssheng", pd.getString("province"));
		// 市
		pd.put("addressshi", pd.getString("city"));
		// 区
		pd.put("addressqu", pd.getString("county"));
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		companyService.edit(pd);
		mv.setViewName("information/company/company_edit");
		mv.addObject("msg", "edit");
		mv.addObject("pd", pd);
		return mv;
	}

	/**
	 * 物理删除
	 */
	@RequestMapping(value = "/delete")
	public void delete(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			companyService.delete(pd);
			out.write("success");
			out.close();
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

	}

	/**
	 * 
	 * TODO 方法作用：系统分配角色
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-23 下午5:08:43
	 */
	@ResponseBody
	@RequestMapping(value = "/CompanyRoleTree")
	public Object CompanyRoleTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		List<PageData> list = new ArrayList<PageData>();
		try {
			if (pd.getString("companyIdForDepart") != null) {
				list = companyService.listdepart(pd);
			} else if (pd.getString("companyIdForRole") != null) {
				list = companyService.listrole(pd);
			} else {
				list = companyService.listcompanyAll(pd);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("result", "success");
		map.put("data", list);
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 
	 * TODO 方法作用：公司分配角色
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-23 下午5:08:39
	 */
	@ResponseBody
	@RequestMapping(value = "/userCompanyRoleTree")
	public Object userCompanyRoleTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		List<PageData> list = new ArrayList<PageData>();
		try {
			if (pd.getString("companyIdForDepart") != null) {
				list = companyService.listdepart(pd);
			} else if (pd.getString("companyIdForRole") != null) {
				list = companyService.listrole(pd);
			} else {
				pd.put("id", getSessionCompanyid());
				list = companyService.listcompanyAll(pd);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("result", "success");
		map.put("data", list);
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 
	 * TODO 方法作用：权限操作
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:19:32
	 */
	/* ===============================权限================================== */
	public Map<String, String> getHC() {
		Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
